﻿package {
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.media.*;
	public class Main extends Sprite {
		private var video:Video = new Video();
		private var connection:NetConnection;
		private var stream:NetStream;
		private var cam:Camera;
		private var mic:Microphone;
		public function Main() {
			cam = Camera.getCamera();
			if (cam != null) {
				video.smoothing = true;
				video.attachCamera(cam);
				video.x = (stage.stageWidth-video.width)/2;
				video.y = (stage.stageHeight-video.height)/2;
				addChild(video);
				connect();
			}
		}
		private function connect():void {
			connection = new NetConnection();
			connection.connect("rtmp://localhost/flv/");
			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			//trace("11");
		}
		private function netStatusHandler(e:NetStatusEvent):void {
			trace(e.info.code);
			switch (e.info.code) {
				case "NetConnection.Connect.Success" :
					connectStream();
					break;
				case "NetConnection.Connect.Failed" :
					break;
				default:
					
			}
		}
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		private function connectStream():void {
			stream = new NetStream(connection);
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			mic = Microphone.getMicrophone();
			stream.attachAudio(mic);
			stream.attachCamera(cam);
			stream.publish("filename", "record");
		}

	}
}